<template>
  <div id="addPatient">
    <header>
      <span @click="goBack()">取消</span>
      <div v-if="status == 'add'">新建患者</div>
      <div v-if="status == 'edit'">编辑患者</div>
      <span @click='save'>保存</span>
    </header>
    <div class='box'>
      <ul>
        <li>
          <span>姓名:</span>
          <input type="text" placeholder="请填写患者真实姓名" v-model='obj.name'>
        </li>
        <li>
          <span>性别:</span>
          <label>
            <input type="radio" v-model='obj.sex' value="女">
            <i></i>女
          </label>
          <label>
            <input type="radio" v-model='obj.sex' value="男">
            <i></i>男
          </label>
        </li>
        <li>
          <span>手机号:</span>
          <input type="text" placeholder="添加患者手机号" v-model='obj.tele'>
        </li>
        <li>
          <span>年龄:</span>
          <input type="number" placeholder="添加年龄" v-model='obj.age'>
        </li>
      </ul>
    </div>

  </div>
</template>
<script>
  export default {
    data() {
      return {
        pid: '', //患者id
        status: '', //新建还是编辑
        obj: {
          userid: localStorage.getItem("loginMobile"),
          name: '', //患者姓名
          sex: '', //性别
          tele: '', //手机号
          age: '', //年龄
        },
      }
    },
    methods: {
      //返回上一级
      goBack() {
        this.$router.back();
      },
      init() {
        if (this.$route.query.status) {
          this.status = this.$route.query.status;
        }
        if (this.$route.query.id) {
          this.pid = this.$route.query.id;
          this.$api.get(this.$api.baseURLSchool, "select_cid/patient/" + this.pid, {}, r => {
            // console.log(r);
            if (r.data.ret == 1) {
              this.obj = r.data.info;
            }
          }, err => {
            console.log(err);
          })
        }
      },
      //保存
      save() {
          // 新建患者
          if (this.obj.name && this.obj.sex && this.obj.age) {
            if (this.obj.tele) {
              if (!(/^1[34578]\d{9}$/.test(this.obj.tele))) {
                this.$utils._toast("手机号码不合法");
                return false;
              }
            }
            if (this.status == "add") {
              this.$api.post(this.$api.baseURLSchool, "updatedata/patient/add", this.obj, r => {
                if (r.data.ret == 1) {
                  this.$utils._toast("保存成功");
                  setTimeout(() => {
                    this.$router.back();
                  }, 300);
                }
              }, err => {
                console.log(err);
              })
            } else if (this.status == "edit") {
              this.$api.post(this.$api.baseURLSchool, "updatedata/patient/edit", this.obj, r => {
                if (r.data.ret == 1) {
                  this.$utils._toast("保存成功");
                  setTimeout(() => {
                    this.$router.back();
                  }, 300);
                }
              }, err => {
                console.log(err);
              })
            }
          } else {
            this.$utils._toast("以上信息请填写完整");
          }
        }
    },
  created() {
    this.init();
  }
  }

</script>
<style lang="scss" scoped>


</style>
